package com.ht.baselibs.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Environment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.ht.baselibs.R;
import com.ht.baselibs.bean.ApkInfoBean;
import com.ht.baselibs.constant.FileConfig;
import com.ht.baselibs.constant.SPConfig;
import com.ht.baselibs.services.DownloadApkSevice;
import com.umeng.analytics.MobclickAgent;

import java.io.File;

public class UpdateDialogUtils {

    public static final int FORCED_UPDATE_LENG = 4;

    public static Dialog showUpdateDialog(final Activity act, final ApkInfoBean apkInfoBean, final ProgressDialog progressDialog) {
        final Dialog updateDialog = new Dialog(act, R.style.umengUpdateDialog);
        View view = View.inflate(act, R.layout.dialog_umeng_update, null);
        updateDialog.setContentView(view);
        updateDialog.setCancelable(false);

        ((TextView)view.findViewById(R.id.tv_dialog_umeng_update_nowversion)).setText(act.getResources().getString(R.string.UMNowVersion) + DeviceUtils.getAppVersionName());
        ((TextView)view.findViewById(R.id.tv_dialog_umeng_update_newversion)).setText(act.getResources().getString(R.string.UMNewVersion) + apkInfoBean.versionName);
        ((TextView)view.findViewById(R.id.tv_dialog_umeng_update_updatelog)).setText(apkInfoBean.description);
        TextView cancelBtn = (TextView) view.findViewById(R.id.btn_dialog_umeng_update_cancel);

        CheckBox ignoreView = (CheckBox) view.findViewById(R.id.cb_dialog_umeng_update_ignore);
        ignoreView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    SharedPreferencesUtils.put(SPConfig.IGNORE_APP_UPDATE_VERSION, apkInfoBean.versionName);
                }
            }
        });

        final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + FileConfig.BASE_APK_FILE_DIR + act.getPackageName() +"/"+ MD5Utils.encode(apkInfoBean.versionName), DownloadApkSevice.APK_FILE_NAME);
        if (file.exists()){
            view.findViewById(R.id.tv_dialog_umeng_update_downloaded).setVisibility(View.VISIBLE);
        }

        String[] versionStr = apkInfoBean.versionName.split("\\.");
        final boolean isForcedUpdate = versionStr.length >= FORCED_UPDATE_LENG;
        if (isForcedUpdate){
            cancelBtn.setText("退出程序");
            ignoreView.setVisibility(View.GONE);
        } else {
            ignoreView.setVisibility(View.VISIBLE);
        }

        view.findViewById(R.id.btn_dialog_umeng_update_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!file.exists()) {
                    Intent downloadIntent = new Intent(act, DownloadApkSevice.class);
                    downloadIntent.putExtra(DownloadApkSevice.APK_URL, apkInfoBean.versionUrl);
                    downloadIntent.putExtra(DownloadApkSevice.APK_VERSION, apkInfoBean.versionName);
                    act.startService(downloadIntent);
                    progressDialog.show();
                } else {
                    DeviceUtils.installApk(act, file.getAbsolutePath());
                }

                updateDialog.dismiss();
                if (!isForcedUpdate) {
                    progressDialog.setCancelable(true);
                } else {
                    progressDialog.setCancelable(false);
                }
            }
        });

        view.findViewById(R.id.btn_dialog_umeng_update_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateDialog.dismiss();
                if (isForcedUpdate){
                    //退出程序
                    MobclickAgent.onKillProcess(act);
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }
        });

        updateDialog.show();
        return updateDialog;
    }

}
